package cn.kgc.fcm.springbootmybatisplusexample.controller;

import cn.kgc.fcm.springbootmybatisplusexample.dto.ResultDTO;
import cn.kgc.fcm.springbootmybatisplusexample.entity.Menu;
import cn.kgc.fcm.springbootmybatisplusexample.service.MenuService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/menu")
public class MenuController {

    private Logger log = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    private MenuService menuService;

    @GetMapping("/show_menu")
    public String showMenu(Model model, Page<Menu> page){

        //Page对象默认每页10条，设置自己需要的每页条数
        page.setSize(4);

        model.addAttribute("pager",page);

        log.info("总条数：{}",page.getTotal());
        log.info("总页数：{}",page.getPages());
        log.info("当前页:{}",page.getCurrent());
        log.info("每页{}条",page.getSize());


        ResultDTO<List<Menu>> menuListResultDTO = menuService.selectMenuAllPage(page);
        model.addAttribute("result",menuListResultDTO);
        return "menu/show_menu";
    }

    @GetMapping("/show_menu/{id}")
    public String showMenu(@PathVariable Integer id,Model model){
        ResultDTO<Menu> menuResultDTO = menuService.selectMenuById(id);
        model.addAttribute("result",menuResultDTO);
        return "menu/show_menu_one";
    }

    @GetMapping("/add_menu")
    public String addMenu(){
        return "menu/add_menu";
    }

    @PostMapping("/do_add_menu")
    public String doAddMenu(Menu menu){
        ResultDTO resultDTO = menuService.addMenu(menu);
        return "redirect:/menu/show_menu";
    }
}
